package com.example.ucenter.controller;


import com.example.commonutils.JwtUtils;
import com.example.commonutils.ResultData;
import com.example.commonutils.ordervo.MemberForOrder;
import com.example.ucenter.entity.Member;
import com.example.ucenter.entity.vo.MemberVo;
import com.example.ucenter.entity.vo.RegisterVo;
import com.example.ucenter.service.MemberService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-01
 */
@RestController
@RequestMapping("/ucenter/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private StringRedisTemplate redisTemplate;

//    private Integer loginCount=0;

    //根据日期获取当天的注册人数
    @GetMapping(value = "countRegister/{date}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Integer countRegister(@PathVariable String date){
        Integer count=memberService.countRegisterDate(date);
        return count;
    }

    //获取登录人数
    @GetMapping("getLoginCount")
    public Integer LoginCount(){
        String loginCount = redisTemplate.opsForValue().get("loginCount");
        //统计之后将数据初始化
        redisTemplate.opsForValue().set("loginCount","0");
        return Integer.parseInt(loginCount);
    }

    //登录
    @PostMapping(value = "login",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData login(@RequestBody Member member) {
        //调用service
        //返回值为token
        String token = memberService.login(member);
        return ResultData.ok().data("token", token);
    }

    //注册
    @PostMapping(value = "register",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return ResultData.ok();
    }

    //注册
    @PostMapping(value = "forget",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData forget(@RequestBody RegisterVo registerVo) {
        memberService.forget(registerVo);
        return ResultData.ok();
    }

    //根据token返回用户数据
    @GetMapping(value = "getMemberInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getById(memberId);
        MemberVo memberVo = new MemberVo();
        if (member!=null) {
            BeanUtils.copyProperties(member,memberVo);
        }
        return ResultData.ok().data("userInfo",memberVo);
    }

    //根据id获取用户信息
    @PostMapping(value = "getInfoUc/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public MemberVo getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        Member member = memberService.getById(id);
        MemberVo memberVo = new MemberVo();
        BeanUtils.copyProperties(member,memberVo);
        return memberVo;
    }

    //根据用户id获取用户信息
    @GetMapping(value = "getUserForOrder/{userId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public MemberForOrder getUserForOrder(@PathVariable String userId){
        //根据id拿到用户信息
        Member member = memberService.getById(userId);
//        定义服务间通用对象类型
        MemberForOrder memberForOrder =new MemberForOrder();
        //复制属性
        BeanUtils.copyProperties(member,memberForOrder);
        return memberForOrder;
    }

    @GetMapping("getUserInfo/{memberId}")
    public Map<String,String> getUserInfo(@PathVariable String memberId){
        Member member = memberService.getById(memberId);
        Map<String, String> map = new HashMap<>();
        map.put("nickname",member.getNickname());
        map.put("avatar",member.getAvatar());
        return map;
    }
}

